<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="交易流水号">
            <a-input v-decorator="['transactionNo']" placeholder="请输入交易流水号"/>
          </a-form-item>
          <a-form-item label="关联业务单号">
            <a-input v-decorator="['relatedNo']" placeholder="请输入关联业务单号"/>
          </a-form-item>
          <a-form-item label="交易规则">
            <a-select v-decorator="['ruleCode']" :options="ruleList" placeholder="请选择交易规则">
            </a-select>
          </a-form-item>
          <a-form-item label="交易时间">
            <a-range-picker v-decorator="['between']" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>
    <s-table
        ref="table"
        rowKey="transactionNo"
        :loading="isLoading"
        :columns="columns"
        :data="loadData"
        :pageSize="15"
    >
      <!-- 会员信息 -->
      <template v-slot:user="text, item">
        <UserItem v-if="text" :user="text"/>
        <div v-else>{{ item.userId }}</div>
      </template>
      <!-- 变动数量 -->
      <template v-slot:points="text">
        <p class="c-p">{{ text  | fixed }}</p>
      </template>
      <!-- 变动数量 -->
      <template v-slot:status="text">
        <a-tag :color="TransactionStatusEnum[text].color">
          {{ TransactionStatusEnum[text].name }}
        </a-tag>
      </template>
      <!-- 交易类型 -->
      <template v-slot:ruleCode="text">
        <a-tag>{{ text | select(ruleList) }}</a-tag>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import * as Api from '@/api/market/points'
import {STable, UserItem} from '@/components/Table'
import TransactionStatusEnum from "@/common/enum/points/TransactionStatus";

export default {
  name: 'Index',
  components: {
    STable,
    UserItem
  },
  data() {
    return {
      // 交易状态枚举
      TransactionStatusEnum,
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '会员信息',
          dataIndex: 'user',
          scopedSlots: {customRender: 'user'}
        },
        {
          title: "交易单号",
          dataIndex: 'transactionNo'
        },
        {
          title: '数量',
          dataIndex: 'points',
          scopedSlots: {customRender: 'points'}
        },
        {
          title: "关联业务单号",
          dataIndex: 'relatedNo'
        },
        {
          title: '交易规则',
          dataIndex: 'ruleCode',
          scopedSlots: {customRender: 'ruleCode'}
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: {customRender: 'status'}
        },
        {
          title: '描述/说明',
          dataIndex: 'description'
        },
        {
          title: '变动时间',
          dataIndex: 'creationTime'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.transaction({...param, ...this.queryParam})
            .then(response => response)
      },
      ruleList: []
    }
  },
  created() {
    this.getRuleList();
  },
  methods: {
    // 获取积分规则列表
    getRuleList() {
      Api.ruleSelect().then(response => {
        console.log(response)
        this.ruleList = response.items
      })
    },
    /**
     * 刷新列表
     * @param {Boolean} bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 确认搜索
    handleSearch(e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = {...this.queryParam, ...values}
          this.handleRefresh(true)
        }
      })
    }

  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
